package com.hys.controller;


import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hys.common.Result;
import com.hys.exception.ServiceException;
import com.hys.param.CooperAddOrUpdateParam;
import com.hys.param.CooperationPageParam;
import com.hys.service.CoopAuditService;
import com.hys.service.CoopInfoService;
import com.hys.vo.CooperInfoVO;
import com.hys.vo.CooperationPageVO;
import com.hys.vo.FileImageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 用户信息
 *
 * @author cheer
 */
@RestController
@Api(tags = "合作信息")
@RequestMapping("/cooperation")
public class CooperationController {

    @Autowired
    private CoopInfoService coopInfoService;

    @Autowired
    private CoopAuditService coopAuditService;

    @SaCheckRole(value = {"admin","dept-admin","audit"}, mode = SaMode.OR)
    @PostMapping("page")
    @ApiOperation(value = "列表", notes = "未完成权限内容")
    public Result<IPage<CooperationPageVO>> page(@RequestBody CooperationPageParam cooperationPage) {
        IPage<CooperationPageVO> page = coopInfoService.page(cooperationPage);
        return Result.success(page);
    }

    @PostMapping("add")
    @ApiOperation(value = "添加")
    @SaCheckRole(value = {"admin","dept-admin"}, mode = SaMode.OR)
    public Result add(@RequestBody @Validated CooperAddOrUpdateParam addOrUpdateParam) {
        coopInfoService.add(addOrUpdateParam);
        return Result.success();
    }

    @PostMapping("upload")
    @ApiOperation(value = "文件图片")
    @SaCheckRole(value = {"admin","dept-admin"}, mode = SaMode.OR)
    public Result<FileImageVO> upload(@RequestBody MultipartFile file) {
        FileImageVO fileImageVO = coopInfoService.upload(file);
        return Result.success(fileImageVO);
    }

    @GetMapping("info")
    @ApiOperation(value = "查看")
    public Result<CooperInfoVO> info(Integer id) {
        CooperInfoVO cooperInfoVO = coopInfoService.info(id);
        return Result.success(cooperInfoVO);
    }

    @PostMapping("update")
    @ApiOperation(value = "修改")
    @SaCheckRole(value = {"admin","dept-admin"}, mode = SaMode.OR)
    public Result update( @RequestBody @Valid CooperAddOrUpdateParam addOrUpdateParam) {
        coopInfoService.update(addOrUpdateParam);
        return Result.success();
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除")
    @SaCheckRole(value = {"admin","dept-admin"}, mode = SaMode.OR)
    public Result delete(@RequestBody List<Integer> cooperIds) {
        coopInfoService.delete(cooperIds);
        return Result.success();
    }

    @GetMapping("audited")
    @ApiOperation(value = "审批")
    @SaCheckRole(value = {"audit"}, mode = SaMode.OR)
    public Result audited(Integer id, Integer status) {
        coopAuditService.audited(id, status);
        return Result.success();
    }

    @GetMapping("download")
    @ApiOperation(value = "下载", produces = "application/octet-stream")
    @SaCheckRole(value = {"admin","dept-admin","audit"}, mode = SaMode.OR)
    public void download( CooperationPageParam cooperationPage, HttpServletResponse response) {
        coopInfoService.download(cooperationPage, response);
    }


    @PostMapping("toLead")
    @ApiOperation(value = "导入")
    @SaCheckRole(value = {"admin"}, mode = SaMode.OR)
    public void toLead( MultipartFile file ) throws IOException {
        coopInfoService.toLead(file);
    }

}
